
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    
<div class="archive_info">
<h1>JavaScript let 和 const
</h1>
<h3>ECMAScript 2015(ECMAScript 6)</h3>
<p>ES2015(ES6) 新增加了两个重要的 JavaScript 关键字: <strong>let</strong> 和 <strong>const</strong>。</p>
<p>let 声明的变量只在 let 命令所在的代码块内有效。 </p>
<p>const 声明一个只读的常量，一旦声明，常量的值就不能改变。</p>
<p>在 ES6 之前，JavaScript 只有两种作用域： <strong>全局变量</strong> 与 <strong>函数内的局部变量</strong>。 </p>
<hr/><h2>全局变量</h2>
<p>在函数外声明的变量作用域是全局的：</p>
<div class="example">
<h2 class="example">实例</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">carName</span><span class="hl-code"> = </span><span class="hl-quotes">"</span><span class="hl-string">Volvo</span><span class="hl-quotes">"</span><span class="hl-code">;
 
</span><span class="hl-comment">//</span><span class="hl-comment"> 这里可以使用 carName 变量</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">myFunction</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-comment">//</span><span class="hl-comment"> 这里也可以使用 carName 变量</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-brackets">}</span></div>
</div>
<br/><a class="tryitbtn"  rel="noopener" target="_blank">尝试一下 »</a>
</div>
<p>全局变量在 JavaScript 程序的任何地方都可以访问。</p>
<hr/><h2>局部变量</h2>
<p>在函数内声明的变量作用域是局部的（函数内）：</p>
<div class="example">
<h2 class="example">实例</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-comment">//</span><span class="hl-comment"> 这里不能使用 carName 变量</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-reserved">function</span><span class="hl-code"> </span><span class="hl-identifier">myFunction</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">carName</span><span class="hl-code"> = </span><span class="hl-quotes">"</span><span class="hl-string">Volvo</span><span class="hl-quotes">"</span><span class="hl-code">;
    </span><span class="hl-comment">//</span><span class="hl-comment"> 这里可以使用 carName 变量</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-comment">//</span><span class="hl-comment"> 这里不能使用 carName 变量</span><span class="hl-comment"></span></div>
</div>
<br/><a class="tryitbtn"  rel="noopener" target="_blank">尝试一下 »</a>
</div>
<p>函数内使用 var 声明的变量只能在函数内访问，如果不使用 var 则是全局变量。</p>
<hr/><h2>
JavaScript 块级作用域(Block Scope)</h2>
<p>使用 var 关键字声明的变量不具备块级作用域的特性，它在 {} 外依然能被访问到。</p>
<pre>{ 
    var x = 2; 
}
// 这里可以使用 x 变量</pre>
<p>在 ES6 之前，是没有块级作用域的概念的。</p>
<p>ES6 可以使用 let 关键字来实现块级作用域。</p>
<p>let 声明的变量只在 let 命令所在的代码块 <strong>{}</strong> 内有效，在 <strong>{}</strong> 之外不能访问。</p>
<pre>{ 
    let x = 2;
}
// 这里不能使用 x 变量</pre>
<hr/>
<h2>重新定义变量</h2>
<p>使用 var 关键字重新声明变量可能会带来问题。</p>
<p>在块中重新声明变量也会重新声明块外的变量：</p>
<div class="example">
<h2 class="example">实例</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">x</span><span class="hl-code"> = </span><span class="hl-number">10</span><span class="hl-code">;
</span><span class="hl-comment">//</span><span class="hl-comment"> 这里输出 x 为 10</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">x</span><span class="hl-code"> = </span><span class="hl-number">2</span><span class="hl-code">;
    </span><span class="hl-comment">//</span><span class="hl-comment"> 这里输出 x 为 2</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-comment">//</span><span class="hl-comment"> 这里输出 x 为 2</span><span class="hl-comment"></span></div>
</div>
<br/><a class="tryitbtn"  rel="noopener" target="_blank">尝试一下 »</a>
</div>
<p>let 关键字就可以解决这个问题，因为它只在 let 命令所在的代码块 <strong>{}</strong> 内有效。</p>
<div class="example">
<h2 class="example">实例</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">x</span><span class="hl-code"> = </span><span class="hl-number">10</span><span class="hl-code">;
</span><span class="hl-comment">//</span><span class="hl-comment"> 这里输出 x 为 10</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-identifier">let</span><span class="hl-code"> </span><span class="hl-identifier">x</span><span class="hl-code"> = </span><span class="hl-number">2</span><span class="hl-code">;
    </span><span class="hl-comment">//</span><span class="hl-comment"> 这里输出 x 为 2</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-comment">//</span><span class="hl-comment"> 这里输出 x 为 10</span><span class="hl-comment"></span></div>
</div>
<br/><a class="tryitbtn"  rel="noopener" target="_blank">尝试一下 »</a>
</div>
<h3>浏览器支持</h3>
<p>Internet Explorer 11 及更早版本的浏览器不支持 let 关键字。</p>
<p>下表列出了各个浏览器支持 let 关键字的最低版本号。</p>
<table class="browserref">
<tbody><tr style="height:50px;">
<th class="bsChrome" style="width:20%;" title="Chrome"></th>
<th class="bsEdge" style="width:20%;" title="Edge"></th>
<th class="bsFirefox" style="width:20%;" title="Firefox"></th>
<th class="bsSafari" style="width:20%;" title="Safari"></th>
<th class="bsOpera" style="width:20%;" title="Opera"></th>
</tr>
<tr>
<td>Chrome 49</td>
<td>IE / Edge 12</td>
<td>Firefox 44</td>
<td>Safari 11</td>
<td>Opera 36</td>
</tr>
<tr>
<td>Mar, 2016</td>
<td>Jul, 2015</td>
<td>Jan, 2015</td>
<td>Sep, 2017</td>
<td>Mar, 2016</td>
</tr></tbody>
</table>
<hr/>
<h2>循环作用域</h2>
<p>使用 var 关键字：</p>
<div class="example">
<h2 class="example">实例</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">i</span><span class="hl-code"> = </span><span class="hl-number">5</span><span class="hl-code">;
</span><span class="hl-reserved">for</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">i</span><span class="hl-code"> = </span><span class="hl-number">0</span><span class="hl-code">; </span><span class="hl-identifier">i</span><span class="hl-code"> &lt; </span><span class="hl-number">10</span><span class="hl-code">; </span><span class="hl-identifier">i</span><span class="hl-code">++</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-comment">//</span><span class="hl-comment"> 一些代码...</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-comment">//</span><span class="hl-comment"> 这里输出 i 为 10</span><span class="hl-comment"></span></div>
</div>
<br/><a class="tryitbtn"  rel="noopener" target="_blank">尝试一下 »</a>
</div>
<p>使用 let 关键字：</p>
<div class="example">
<h2 class="example">实例</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">i</span><span class="hl-code"> = </span><span class="hl-number">5</span><span class="hl-code">;
</span><span class="hl-reserved">for</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">let</span><span class="hl-code"> </span><span class="hl-identifier">i</span><span class="hl-code"> = </span><span class="hl-number">0</span><span class="hl-code">; </span><span class="hl-identifier">i</span><span class="hl-code"> &lt; </span><span class="hl-number">10</span><span class="hl-code">; </span><span class="hl-identifier">i</span><span class="hl-code">++</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-comment">//</span><span class="hl-comment"> 一些代码...</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-comment">//</span><span class="hl-comment"> 这里输出 i 为 5</span><span class="hl-comment"></span></div>
</div>
<br/><a class="tryitbtn"  rel="noopener" target="_blank">尝试一下 »</a>
</div>
<p>在第一个实例中，使用了 <strong>var</strong> 关键字，它声明的变量是全局的，包括循环体内与循环体外。 </p>
<p>在第二个实例中，使用 <strong>let</strong> 关键字， 它声明的变量作用域只在循环体内，循环体外的变量不受影响。 </p>
<hr/>
<h2>局部变量</h2>
<p>在函数体内使用 <strong>var</strong> 和 <strong>let</strong> 关键字声明的变量有点类似。</p>
<p>它们的作用域都是 <strong>局部的</strong>:</p>
<pre>// 使用 var
function myFunction() {
    var carName = "Volvo";   // 局部作用域
}

// 使用 let
function myFunction() {
    let carName = "Volvo";   //  局部作用域
}</pre>
<hr/>
<h2>全局变量</h2>
<p>在函数体外或代码块外使用 <strong>var</strong> 和 <strong>let</strong> 关键字声明的变量也有点类似。</p>
<p>它们的作用域都是 <strong>全局的</strong>:</p>
<pre>// 使用 var
var x = 2;       // 全局作用域

// 使用 let
let x = 2;       // 全局作用域</pre>
<hr/><h2>HTML 代码中使用全局变量</h2>
<p>在 JavaScript 中,  全局作用域是针对 JavaScript 环境。 </p>
<p>在 HTML 中, 全局作用域是针对 window 对象。</p>
<p>使用 <b>var</b> 关键字声明的全局作用域变量属于 window 对象:</p>
<div class="example">
<h2 class="example">实例</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">carName</span><span class="hl-code"> = </span><span class="hl-quotes">"</span><span class="hl-string">Volvo</span><span class="hl-quotes">"</span><span class="hl-code">;
</span><span class="hl-comment">//</span><span class="hl-comment"> 可以使用 window.carName 访问变量</span><span class="hl-comment"></span></div>
</div>
<br/><a class="tryitbtn"  rel="noopener" target="_blank">尝试一下 »</a>
</div>
<p>使用 <b>let</b> 关键字声明的全局作用域变量不属于 window 对象:</p>
<div class="example">
<h2 class="example">实例</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-identifier">let</span><span class="hl-code"> </span><span class="hl-identifier">carName</span><span class="hl-code"> = </span><span class="hl-quotes">"</span><span class="hl-string">Volvo</span><span class="hl-quotes">"</span><span class="hl-code">;
</span><span class="hl-comment">//</span><span class="hl-comment"> 不能使用 window.carName 访问变量</span><span class="hl-comment"></span></div>
</div>
<br/><a class="tryitbtn"  rel="noopener" target="_blank">尝试一下 »</a>
</div>
<h3>重置变量</h3>
<p>使用 <strong>var</strong> 关键字声明的变量在任何地方都可以修改：</p>
<div class="example">
<h2 class="example">实例</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">x</span><span class="hl-code"> = </span><span class="hl-number">2</span><span class="hl-code">;
 
</span><span class="hl-comment">//</span><span class="hl-comment"> x 为 2</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">x</span><span class="hl-code"> = </span><span class="hl-number">3</span><span class="hl-code">;
 
</span><span class="hl-comment">//</span><span class="hl-comment"> 现在 x 为 3</span><span class="hl-comment"></span></div>
</div>
<br/><a class="tryitbtn"  rel="noopener" target="_blank">尝试一下 »</a>
</div>
<p>在相同的作用域或块级作用域中，不能使用 <strong>let</strong> 关键字来重置 <strong>var</strong> 关键字声明的变量:</p>
<pre>var x = 2;       // 合法
let x = 3;       // 不合法

{
    var x = 4;   // 合法
    let x = 5   // 不合法
}</pre>
<p>在相同的作用域或块级作用域中，不能使用 <strong>let</strong> 关键字来重置 <strong>let</strong> 关键字声明的变量:</p>
<pre>let x = 2;       // 合法
let x = 3;       // 不合法

{
    let x = 4;   // 合法
    let x = 5;   // 不合法
}</pre>
<p>在相同的作用域或块级作用域中，不能使用 <strong>var</strong> 关键字来重置 <strong>let</strong> 关键字声明的变量:</p>
<pre>let x = 2;       // 合法
var x = 3;       // 不合法

{
    let x = 4;   // 合法
    var x = 5;   // 不合法
}</pre>
<p> <strong>let</strong> 关键字在不同作用域，或不同块级作用域中是可以重新声明赋值的:</p>
<pre>let x = 2;       // 合法

{
    let x = 3;   // 合法
}

{
    let x = 4;   // 合法
}</pre>
<hr/>
<h2>变量提升</h2>
<p>JavaScript 中，var 关键字定义的变量可以在使用后声明，也就是变量可以先使用再声明（<a  rel="noopener" target="_blank">JavaScript 变量提升</a>）。</p>
<div class="example">
<h2 class="example">实例</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-comment">//</span><span class="hl-comment"> 在这里可以使用 carName 变量</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">carName</span><span class="hl-code">;</span></div>
</div>
<br/><a class="tryitbtn"  rel="noopener" target="_blank">尝试一下 »</a>
</div>
<p>let 关键字定义的变量则不可以在使用后声明，也就是变量需要先声明再使用。</p>
<pre>// 在这里不可以使用 carName 变量

let carName;</pre>
<hr/>
<h2>const 关键字</h2>
<p>const 用于声明一个或多个常量，声明时必须进行初始化，且初始化后值不可再修改：</p>
<div class="example">
<h2 class="example">实例</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-reserved">const</span><span class="hl-code"> </span><span class="hl-identifier">PI</span><span class="hl-code"> = </span><span class="hl-number">3.141592653589793</span><span class="hl-code">;
</span><span class="hl-identifier">PI</span><span class="hl-code"> = </span><span class="hl-number">3.14</span><span class="hl-code">;      </span><span class="hl-comment">//</span><span class="hl-comment"> 报错</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-identifier">PI</span><span class="hl-code"> = </span><span class="hl-identifier">PI</span><span class="hl-code"> + </span><span class="hl-number">10</span><span class="hl-code">;   </span><span class="hl-comment">//</span><span class="hl-comment"> 报错</span><span class="hl-comment"></span></div>
</div>
<br/><a class="tryitbtn"  rel="noopener" target="_blank">尝试一下 »</a>
</div>
<p><code>const</code>定义常量与使用<code>let</code> 定义的变量相似：</p>
<ul>
<li>二者都是块级作用域</li>
<li>都不能和它所在作用域内的其他变量或函数拥有相同的名称</li>
</ul>
<p>两者还有以下两点区别：</p>
<ul>
<li><code>const</code>声明的常量必须初始化，而<code>let</code>声明的变量不用</li>
<li>const 定义常量的值不能通过再赋值修改，也不能再次声明。而 let 定义的变量值可以修改。</li>
</ul>
<pre>var x = 10;
// 这里输出 x 为 10
{ 
    const x = 2;
    // 这里输出 x 为 2
}
// 这里输出 x 为 10</pre>
<p>const 声明的常量必须初始化：</p>
<pre>// 错误写法
const PI;
PI = 3.14159265359;

// 正确写法
const PI = 3.14159265359;</pre>
<hr/>
<h2>并非真正的常量</h2>
<p>const 的本质: const 定义的变量并非常量，并非不可变，它定义了一个常量引用一个值。使用 const 定义的对象或者数组，其实是可变的。下面的代码并不会报错：</p>
<div class="example">
<h2 class="example">实例</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-comment">//</span><span class="hl-comment"> 创建常量对象</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-reserved">const</span><span class="hl-code"> </span><span class="hl-identifier">car</span><span class="hl-code"> = </span><span class="hl-brackets">{</span><span class="hl-identifier">type</span><span class="hl-code">:</span><span class="hl-quotes">"</span><span class="hl-string">Fiat</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-identifier">model</span><span class="hl-code">:</span><span class="hl-quotes">"</span><span class="hl-string">500</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-identifier">color</span><span class="hl-code">:</span><span class="hl-quotes">"</span><span class="hl-string">white</span><span class="hl-quotes">"</span><span class="hl-brackets">}</span><span class="hl-code">;
 
</span><span class="hl-comment">//</span><span class="hl-comment"> 修改属性:</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-identifier">car</span><span class="hl-code">.</span><span class="hl-identifier">color</span><span class="hl-code"> = </span><span class="hl-quotes">"</span><span class="hl-string">red</span><span class="hl-quotes">"</span><span class="hl-code">;
 
</span><span class="hl-comment">//</span><span class="hl-comment"> 添加属性</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-identifier">car</span><span class="hl-code">.</span><span class="hl-identifier">owner</span><span class="hl-code"> = </span><span class="hl-quotes">"</span><span class="hl-string">Johnson</span><span class="hl-quotes">"</span><span class="hl-code">;</span></div>
</div>
<br/><a class="tryitbtn"  rel="noopener" target="_blank">尝试一下 »</a>
</div>
<p>但是我们不能对常量对象重新赋值：</p>
<div class="example">
<h2 class="example">实例</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-reserved">const</span><span class="hl-code"> </span><span class="hl-identifier">car</span><span class="hl-code"> = </span><span class="hl-brackets">{</span><span class="hl-identifier">type</span><span class="hl-code">:</span><span class="hl-quotes">"</span><span class="hl-string">Fiat</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-identifier">model</span><span class="hl-code">:</span><span class="hl-quotes">"</span><span class="hl-string">500</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-identifier">color</span><span class="hl-code">:</span><span class="hl-quotes">"</span><span class="hl-string">white</span><span class="hl-quotes">"</span><span class="hl-brackets">}</span><span class="hl-code">;
</span><span class="hl-identifier">car</span><span class="hl-code"> = </span><span class="hl-brackets">{</span><span class="hl-identifier">type</span><span class="hl-code">:</span><span class="hl-quotes">"</span><span class="hl-string">Volvo</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-identifier">model</span><span class="hl-code">:</span><span class="hl-quotes">"</span><span class="hl-string">EX60</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-identifier">color</span><span class="hl-code">:</span><span class="hl-quotes">"</span><span class="hl-string">red</span><span class="hl-quotes">"</span><span class="hl-brackets">}</span><span class="hl-code">;    </span><span class="hl-comment">//</span><span class="hl-comment"> 错误</span><span class="hl-comment"></span></div>
</div>
<br/><a class="tryitbtn"  rel="noopener" target="_blank">尝试一下 »</a>
</div>
<p>以下实例修改常量数组：</p>
<div class="example">
<h2 class="example">实例</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-comment">//</span><span class="hl-comment"> 创建常量数组</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-reserved">const</span><span class="hl-code"> </span><span class="hl-identifier">cars</span><span class="hl-code"> = </span><span class="hl-brackets">[</span><span class="hl-quotes">"</span><span class="hl-string">Saab</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string">Volvo</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string">BMW</span><span class="hl-quotes">"</span><span class="hl-brackets">]</span><span class="hl-code">;
 
</span><span class="hl-comment">//</span><span class="hl-comment"> 修改元素</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-identifier">cars</span><span class="hl-brackets">[</span><span class="hl-number">0</span><span class="hl-brackets">]</span><span class="hl-code"> = </span><span class="hl-quotes">"</span><span class="hl-string">Toyota</span><span class="hl-quotes">"</span><span class="hl-code">;
 
</span><span class="hl-comment">//</span><span class="hl-comment"> 添加元素</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-identifier">cars</span><span class="hl-code">.</span><span class="hl-identifier">push</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">Audi</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;</span></div>
</div>
<br/><a class="tryitbtn"  rel="noopener" target="_blank">尝试一下 »</a>
</div>
<p>但是我们不能对常量数组重新赋值：</p>
<div class="example">
<h2 class="example">实例</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-reserved">const</span><span class="hl-code"> </span><span class="hl-identifier">cars</span><span class="hl-code"> = </span><span class="hl-brackets">[</span><span class="hl-quotes">"</span><span class="hl-string">Saab</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string">Volvo</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string">BMW</span><span class="hl-quotes">"</span><span class="hl-brackets">]</span><span class="hl-code">;
</span><span class="hl-identifier">cars</span><span class="hl-code"> = </span><span class="hl-brackets">[</span><span class="hl-quotes">"</span><span class="hl-string">Toyota</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string">Volvo</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string">Audi</span><span class="hl-quotes">"</span><span class="hl-brackets">]</span><span class="hl-code">;    </span><span class="hl-comment">//</span><span class="hl-comment"> 错误</span><span class="hl-comment"></span></div>
</div>
<br/><a class="tryitbtn"  rel="noopener" target="_blank">尝试一下 »</a>
</div>
<h3>浏览器支持</h3>
<p>Internet Explorer 10 及更早版本的浏览器不支持 const 关键字。</p>
<p>下表列出了各个浏览器支持 const 关键字的最低版本号。</p>
<table class="browserref">
<tbody><tr style="height:50px;">
<th class="bsChrome" style="width:20%;" title="Chrome"></th>
<th class="bsEdge" style="width:20%;" title="Edge"></th>
<th class="bsFirefox" style="width:20%;" title="Firefox"></th>
<th class="bsSafari" style="width:20%;" title="Safari"></th>
<th class="bsOpera" style="width:20%;" title="Opera"></th>
</tr>
<tr>
<td>Chrome 49</td>
<td>IE / Edge 11</td>
<td>Firefox 36</td>
<td>Safari 10</td>
<td>Opera 36</td>
</tr>
<tr>
<td>Mar, 2016</td>
<td>Oct, 2013</td>
<td>Feb, 2015</td>
<td>Sep, 2016</td>
<td>Mar, 2016</td>
</tr></tbody>
</table>
<h3>重置变量</h3>
<p>使用 <strong>var</strong> 关键字声明的变量在任何地方都可以修改：</p>
<div class="example">
<h2 class="example">实例</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">x</span><span class="hl-code"> = </span><span class="hl-number">2</span><span class="hl-code">;    </span><span class="hl-comment">//</span><span class="hl-comment">  合法</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">x</span><span class="hl-code"> = </span><span class="hl-number">3</span><span class="hl-code">;    </span><span class="hl-comment">//</span><span class="hl-comment">  合法</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-identifier">x</span><span class="hl-code"> = </span><span class="hl-number">4</span><span class="hl-code">;        </span><span class="hl-comment">//</span><span class="hl-comment">  合法</span><span class="hl-comment"></span></div>
</div>
</div>
<p>在相同的作用域或块级作用域中，不能使用 <strong>const</strong> 关键字来重置 <strong>var</strong> 和 <strong>let</strong>关键字声明的变量:</p>
<pre>var x = 2;         // 合法
const x = 2;       // 不合法
{
    let x = 2;     // 合法
    const x = 2;   // 不合法
}</pre>
<p>在相同的作用域或块级作用域中，不能使用 <strong>const</strong> 关键字来重置 <strong>const</strong> 关键字声明的变量:</p>
<pre>const x = 2;       // 合法
const x = 3;       // 不合法
x = 3;             // 不合法
var x = 3;         // 不合法
let x = 3;         // 不合法

{
    const x = 2;   // 合法
    const x = 3;   // 不合法
    x = 3;         // 不合法
    var x = 3;     // 不合法
    let x = 3;     // 不合法
}</pre>
<p> <strong>const</strong> 关键字在不同作用域，或不同块级作用域中是可以重新声明赋值的:</p>
<pre>const x = 2;       // 合法

{
    const x = 3;   // 合法
}

{
    const x = 4;   // 合法
}</pre>
<h3>变量提升</h3>
<p>JavaScript var 关键字定义的变量可以在使用后声明，也就是变量可以先使用再声明（<a  rel="noopener" target="_blank">JavaScript 变量提升</a>）。</p>
<div class="example">
<h2 class="example">实例</h2>
<div class="example_code">
<div class="hl-main"><span class="hl-identifier">carName</span><span class="hl-code"> = </span><span class="hl-quotes">"</span><span class="hl-string">Volvo</span><span class="hl-quotes">"</span><span class="hl-code">;    </span><span class="hl-comment">//</span><span class="hl-comment"> 这里可以使用 carName 变量</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-reserved">var</span><span class="hl-code"> </span><span class="hl-identifier">carName</span><span class="hl-code">;</span></div>
</div>
<br/><a class="tryitbtn"  rel="noopener" target="_blank">尝试一下 »</a>
</div>
<p>const 关键字定义的变量则不可以在使用后声明，也就是变量需要先声明再使用。</p>
<pre>carName = "Volvo";    // 在这里不可以使用 carName 变量
const carName = "Volvo";</pre> <div class="scroll-top"><a >返回顶部</a></div>
</div>

    </body>
    </html>
    